home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / syslog.lha / SysLog_V1.00 / Developer / examples / Spy / Spy.c < prev   
C/C++ Source or Header  |  1995-11-13  |  2KB  |  110 lines

  1. /*
  2.  * syslog.library Spy example
  3.  *
  4.  * This file is public domain.
  5.  *
  6.  * Author: Petri Nordlund <petrin@megabaud.fi>
  7.  *
  8.  * $Id: Spy.c 1.3 1995/11/01 19:07:32 petrin Exp petrin $
  9.  *
  10.  *
  11.  * This program demostrates how to add a spy to syslog.library.
  12.  *
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdarg.h>
  18. #include <proto/exec.h>
  19. #include <dos/dos.h>
  20. #include <proto/syslog.h>
  21. #include <libraries/syslog.h>
  22.  
  23.  
  24.  
  25. struct Library *SysLogBase = NULL;
  26. struct SysLogSpy *spy = NULL;
  27.  
  28.  
  29. int
  30. main(int argc, char **argv)
  31. {
  32.     ULONG spysig;
  33.     BOOL quitting = FALSE;
  34.  
  35.     /*
  36.      * First open the syslog.library
  37.      */
  38.     if(!(SysLogBase = OpenLibrary(SYSLOGNAME, SYSLOGVERSION)))
  39.     {
  40.         puts("Can't open \"syslog.library\".");
  41.         exit(RETURN_FAIL);
  42.     }
  43.  
  44.     /*
  45.      * Tell syslog.library we want to see the log messages too
  46.      */
  47.     if(!(spy=AddSysLogSpy()))
  48.     {
  49.         puts("Can't connect with syslog.library.");
  50.         exit(RETURN_FAIL);
  51.     }
  52.  
  53.     /*
  54.      * Get the signal from message port
  55.      */
  56.     spysig = 1L << spy->Spy_port->mp_SigBit;
  57.  
  58.     /*
  59.      * And now just wait for messages or CTRL_C
  60.      */
  61.     while(!quitting)
  62.     {
  63.         ULONG signals;
  64.  
  65.         signals = Wait(spysig | SIGBREAKF_CTRL_C);
  66.  
  67.         if(signals & spysig)
  68.         {
  69.             struct SysLogMessage *msg;
  70.  
  71.             /*
  72.              * Get all messages
  73.              */
  74.             while(msg=(struct SysLogMessage *) GetMsg(spy->Spy_port))
  75.             {
  76.                 /*
  77.                  * Ask syslog.library to put a time stamp to the message
  78.                  */
  79.                 GetSysLogMsgTime(msg);
  80.  
  81.                 /*
  82.                  * Print the message. There's always \n at the end of the string.
  83.                  * You'll find the message priority and facility in msg->pri.
  84.                  */
  85.                 printf(msg->msg);
  86.  
  87.                 /*
  88.                  * Ask syslog.library to delete the message
  89.                  */
  90.                 DeleteSysLogMessage(msg);
  91.             }
  92.         }
  93.  
  94.         if(signals & SIGBREAKF_CTRL_C)
  95.             quitting = TRUE;
  96.     }
  97.  
  98.     /*
  99.      * We are done. Syslog.library takes care of messages that might still be
  100.      * in the message port.
  101.      */
  102.     if(spy)
  103.         RemSysLogSpy(spy);
  104.  
  105.     if(SysLogBase)
  106.         CloseLibrary(SysLogBase);
  107.  
  108.     return(RETURN_OK);
  109. }
  110.